using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;

namespace tolang
{
    class tolang
    {
        static void Main(string[] args)
        {
            string usage = "Demo of General SQL Parser at http://www.sqlparser.com" + Environment.NewLine
            + "Usage:" + Environment.NewLine
             + "tolang -i insql.txt  -d mssql -t csharp -o outsql.txt" + Environment.NewLine
             + "-i insql.txt, \t input sql file, must be specified" + Environment.NewLine
            + "-d mssql, \t sql engine used to handle the input sql, can be one of: access, db2,mssql,mysql,oracle, if ommitted, mssql engine will be used as default" + Environment.NewLine
            + "-t csharp, \t output type of the input sql, can be  php, vc, vb, pascal(delphi), java,c#,html, html2(use font instead of div), xml" + Environment.NewLine
            + "-o outsql.txt, \t save the output to this file,if ommitted, will write to stand output";


            if (args.Length == 0)
            {
                Console.WriteLine(usage);
                return;
            }

            string inputfile = "";
            string dbvendor = "";
            string outtype = "csharp";
            string outfile = "";

            for (int t = 0; t < args.Length; t++)
            {
                if (args[t] == "-i")
                {
                    if (t + 1 < args.Length)
                    {
                        inputfile = args[t + 1];
                    }
                }

                if (args[t] == "-d")
                {
                    if (t + 1 < args.Length)
                    {
                        dbvendor = args[t + 1];
                    }
                }

                if (args[t] == "-t")
                {
                    if (t + 1 < args.Length)
                    {
                        outtype = args[t + 1];
                    }
                }

                if (args[t] == "-o")
                {
                    if (t + 1 < args.Length)
                    {
                        outfile = args[t + 1];
                    }
                }
            }


            Console.WriteLine("Input sql file: " + inputfile);
            Console.WriteLine("db engine used: " + dbvendor);
            Console.WriteLine("output type: " + outtype);
            Console.WriteLine("output file: " + outfile);

            string lcsqltext = "";

            //Reading from file
            try
            {
                StreamReader re = File.OpenText(inputfile); //new StreamReader(inputfile,new UnicodeEncoding());//
                // Console.WriteLine("The encoding used was {0}.", re.CurrentEncoding);

                string input = null;
                while ((input = re.ReadLine()) != null)
                {
                    lcsqltext = lcsqltext + input + Environment.NewLine;
                    // Console.WriteLine(input);
                }
            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }


            //Create instance of sql parser according to database vendor you selected
            TDbVendor dbv;
            if (dbvendor == "access")
            {
                dbv = TDbVendor.DbVAccess;
            }
            else if (dbvendor == "db2")
            {
                dbv = TDbVendor.DbVDB2;
            }
            else if (dbvendor == "mssql")
            {
                dbv = TDbVendor.DbVMssql;
            }
            else if (dbvendor == "mysql")
            {
                dbv = TDbVendor.DbVMysql;
            }
            else if (dbvendor == "oracle")
            {
                dbv = TDbVendor.DbVOracle;
            }
            else
            {
                dbv = TDbVendor.DbVMssql;
            }

            TGSqlParser sqlparser = new TGSqlParser(dbv);
            Console.WriteLine(lzbasetype.DbVendorStr[(int)dbv]);


            //set the input sql text	
            sqlparser.SqlText.Text = lcsqltext;
            //  aa.FormattedSqlfilename = "aa.txt";

            string outputstr = "";
            Console.WriteLine("Working...");
            // Console.WriteLine(lcsqltext);


            int i = sqlparser.Parse();

            if (i != 0)
            {
                Console.WriteLine("syntax error: " + sqlparser.ErrorMessages);
                return;
            }

           

            switch (outtype)
            {
                case "php":
                    outputstr = sqlparser.ToPhp("var1");
                    break;
                case "java":
                    outputstr = sqlparser.ToJava("var1");
                    break;
                case "javastringbuffer":
                    outputstr = sqlparser.ToJavaStringBuffer("var1");
                    break;
                case "vb":
                    outputstr = sqlparser.ToVb("var1");
                    break;
                case "csharp":
                    outputstr = sqlparser.ToCSharp("var1");
                    break;
                case "csharpstringbuilder":
                    outputstr = sqlparser.ToCSharpStringBuilder("var1");
                    break;
                case "pascal":
                    outputstr = sqlparser.ToPascal("var1");
                    break;
                case "vc":
                    outputstr = sqlparser.ToVc("var1");
                    break;
                default:
                    break;
            }


            if (outfile.Length > 0)
            {
                //save to file
                StreamWriter w = File.CreateText(outfile);
                w.Write(outputstr);
                w.Flush();
                w.Close();
                Console.WriteLine(outfile + " was created");

            }
            else
            {
                Console.WriteLine(outputstr);
            }

        }        
    }
}
